package com.harmonyos.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmonyos.domain.TbFourm;
import com.harmonyos.service.TbFourmService;
import com.harmonyos.mapper.TbFourmMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbFourmServiceImpl extends ServiceImpl<TbFourmMapper, TbFourm>
implements TbFourmService{
    @Resource
    private TbFourmMapper tbFourmMapper;
    @Override
    public String insert(TbFourm fourm) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbFourmMapper.insert(fourm);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String delect(String id) {

        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbFourm fourm = JSONUtil.toBean(id, TbFourm.class);
            tbFourmMapper.deleteById(fourm.getId());
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String select(String user) {

        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbFourm fourm1 = JSONUtil.toBean(user, TbFourm.class);
            TbFourm fourm = tbFourmMapper.selectById(fourm1.getUser());
            objectObjectHashMap.put("data",fourm);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String selecttizi(String tzid) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            QueryWrapper<TbFourm> wrapper = new QueryWrapper<>();
            wrapper.eq("tzid",tzid);
            List<TbFourm> fourm = tbFourmMapper.selectList(wrapper);
            objectObjectHashMap.put("data",fourm);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    /**
     * 查所有
     * @return
     */
    @Override
    public String selectAll() {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            LambdaQueryWrapper<TbFourm> wrapper = new LambdaQueryWrapper<>();
            List<TbFourm> tbFourms = tbFourmMapper.selectList(wrapper);
            objectObjectHashMap.put("data",tbFourms);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }


    @Override
    public String update(TbFourm tbFourm){
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbFourmMapper.updateById(tbFourm);

            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }
}




